<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				customerOwnerCacheKey:"mar-kh-visitPlan-customerOwner-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			formSubmit(){
				let _that = this
				if(!_that.edit){
					_that.om.formSave("visitPlan/doAdd",_that.formdata,_that)
				}else{
					_that.om.formSave("visitPlan/doUpdate",_that.formdata,_that)
				}
			},
			getWorkParam(){
				let _that = this
				return {
					customerOwnerId:_that.$refs.autoform.formdata.customerOwnerId,
					customerOwner:_that.$refs.autoform.formdata.customerOwner
				}
			},
			getParentUserByPost(id){
				let _that = this
				_that.om.request(
					"visitPlan/getParentUserByPost",
					{
						postId:id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.superiorId = res.value.id
					_that.$refs.autoform.formdata.superior = res.value.username
				})
			},
			onSelectPos(item){
				let _that = this
				if(item){
					_that.$refs.autoform.formdata.postId = item.id
					_that.$refs.autoform.formdata.post = item.posname
					_that.$refs.autoform.formdata.instId = item.instId
					_that.$refs.autoform.formdata.inst = item.instName
					_that.$refs.autoform.formdata.instLevelId = item.instCatId
					_that.$refs.autoform.formdata.instLevelName = item.instCatName
					_that.getParentUserByPost(item.id)
				}
			},
			clearPos(){
				let _that = this;
				_that.$refs.autoform.clear('title1', 'postId');
				_that.$refs.autoform.clear('title1', 'instId');
				_that.$refs.autoform.clear('title1', 'instLevelId');
				_that.$refs.autoform.clear('title1', 'superiorId');
			},
			getPost(userId){
				let _that = this
				_that.om.request(
					"orgInstPosUserOfficial/getPosComboboxByUser",
					{ 
						userId:userId,
						flag:true
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title1","postId","data",configSelectData(res,"id","posname"))
					_that.$refs.autoform.initSelectVal("title1","postId")
				})
			},
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				if(fileId){
					ajaxArr.push({
						url:"visitPlan/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor("topinfo","zposId","data",configSelectData(res[0],"id","posname"),mainData)
					changeEditor("title1","customerOwnerId",{
						"link":'../../public/searchUser/searchUser',
						"linkParam":{
							other:JSON.stringify({
							}),
							isSingle:true,
							isUserPos:true,
							isNew:true,
							cachename:_that.customerOwnerCacheKey,
							
							// searchfield: "q",
							// isUserPos:true,
							// cachename:_that.customerOwnerCacheKey,
							// namefield:urlUtil.userPageUrl().namefield,
							// defaultDataCache: _that.defaultDataCache,
							// isNew: true,
							// defaultData: 'searchUserDefaultData',
							// placeholder: urlUtil.userPageUrl().prompt,
							// pagination: true,
							// isSingle:true,
						}
					},null,mainData)
					if(_that.subEditorObj["title2"]){
						let attr = {
							getWorkParam:'getWorkParam'
						}
						let configData = _that.subConfigData["title2"]
						if(hasEditor("title2","customerNameId",configData)){
							attr.titleTemplate = "<%= customerName %>"
						}  
						changeEditor("title2","dataStr",attr,mainData)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[1].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value[index])
								_that.$set(_that.formdata,index,res[1].value[index])
							}
						}
						if(_that.subEditorObj["title2"]){
							let datalist = res[1].value.dataList
							_that.subEditorObj["title2"].workList = datalist
							_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(datalist))
							_that.$set(_that.formdata,"dataStr",JSON.stringify(datalist))
						}
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
							res[0].map((item, index) => {
								if(item.id == _that.$refs.autoform.formdata.zposId){
									_that.$refs.autoform.formdata.postId = item.id
									_that.$refs.autoform.formdata.post = item.posname
									_that.$refs.autoform.formdata.instId = item.instId
									_that.$refs.autoform.formdata.inst = item.instName
									_that.getParentUserByPost(item.id)
								}
							})
						}else{
							_that.$refs.autoform.initSelectIndex("topinfo","zposId")
							if(!_that.$refs.autoform.formdata.postId){
								res[0].map((item, index) => {
									if(item.id == _that.$refs.autoform.formdata.zposId){
										_that.$refs.autoform.formdata.postId = item.id
										_that.$refs.autoform.formdata.post = item.posname
										_that.$refs.autoform.formdata.instId = item.instId
										_that.$refs.autoform.formdata.inst = item.instName
										_that.getParentUserByPost(item.id)
									}
								})
							}
						}
						
						if(!_that.$refs.autoform.formdata.customerOwnerId){
							_that.$refs.autoform.formdata.customerOwnerId = _that.userId
							_that.$refs.autoform.formdata.customerOwner = _that.userName
							_that.getPost(_that.userId)
							_that.getInformationByUserId(_that.userId)
						}
						// if(!_that.$refs.autoform.formdata.customerOwnerId){
						// 	_that.getPost(_that.userId)
						// 	_that.$refs.autoform.formdata.customerOwnerId = _that.userId
						// 	_that.$refs.autoform.formdata.customerOwner = _that.userName
						// }else{
						// 	_that.getPost(_that.$refs.autoform.formdata.customerOwnerId)
						// }
					})
				},options)
			},
			getInformationByUserId(userId){
				let _that = this;
				_that.om.request(
					"orgInstPosUserOfficial/getUserSalaryPosInfo",
					{ 
						userId:userId
					}
				).then(res=>{
					if(res && res.value){
						let obj = res.value;
						let formdata = _that.$refs.autoform.formdata
						formdata.postId = obj.posId 
						formdata.post = obj.posName
						formdata.instId = obj.instId
						formdata.inst = obj.instName
						formdata.instLevelId = obj.instCatId
						formdata.instLevelName = obj.instCatName
						_that.getParentUserByPost(obj.posId)
					}
				})
			},
			clearCustomerOwner(){
				let _that = this;
				_that.$refs.autoform.clear('title1', 'customerOwnerId');
				_that.$refs.autoform.clear('title1', 'postId');
				_that.$refs.autoform.clear('title1', 'instId');
				_that.$refs.autoform.clear('title1', 'instLevelId');
				_that.$refs.autoform.clear('title1', 'superiorId');
			}
		},
		onUnload(){
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				};
				uni.getStorage({
					key:_that.customerOwnerCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data[0]
							let formdata = _that.$refs.autoform.formdata
							formdata.customerOwnerId = d.id 
							formdata.customerOwner = d.username
							_that.getPost(formdata.customerOwnerId)
							_that.getInformationByUserId(formdata.customerOwnerId)
							uni.removeStorage({
								key:_that.customerOwnerCacheKey
							})
						}
					}
				})
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
